package com.ymatou.userregister.testcase;

import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.userregister.parameter.WeiBoUserRegBean;
import com.ymatou.userregister.service.WeiBoUserRegCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymt.utils.tag.P3;
import com.ymttest.database.mongo.UserTokenWapper;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.YMTDateUtil;
import com.ymttest.utils.constant.ECode;

public class Ts_WeiBoUserReg {
	private static WeiBoUserRegBean regBean;
	private static WeiBoUserRegCall regCall;
	private static UserTokenWapper userTokenWapper;
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("微博用户注册 ");
	}

	@Before
	public void caseUp() throws Exception {
		regBean = new WeiBoUserRegBean();
		regCall = new WeiBoUserRegCall();
		userTokenWapper = new UserTokenWapper();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(regCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_WeiBoUserReg_001() {
		Logger.start(true,"创建用户成功");
		try {
			regBean.setSWeiboUserId("BindWeibo_"+TestDataManager.getRandomNum(10));
			regBean.setSName("atuoTest");
			regBean.setSMail(TestDataManager.getEmail());
			regBean.setSMobile(TestDataManager.getPhoneNum());
			regBean.setClientIp("120.1.1.1");
			regBean.setDAddTime(YMTDateUtil.getBeforeOrNextDay(-1));
			regBean.setTerminalSource("ios");
			regBean.setRegSource(1);
			regCall.setData(regBean);
			regCall.callService();		
			
			regCall.setData(regBean);
			regCall.callService();			
			Logger.verifyEquals("登录成功", regCall.getString("SuccessMessage"), "验证返回的Message信息");
			Logger.verifyEquals(ECode.SUCCESS, regCall.getString("ResponseCode"), "验证code");
			Logger.verifyEquals("true", regCall.getString("Success"), "验证是否成功");
			Logger.verifyEquals(14d, regCall.getResult().get("VerifyCode"), "验证凭据(14表示操作成功)");
			Logger.verifyIsNull(regCall.getResult().get("Remark"), "验证remark信息");
			
			Map userToken = userTokenWapper.getUserTokenByUid(((Double)regCall.getResult().get("UserId")).intValue());
			Logger.verifyEquals(userToken.get("sId"), regCall.getResult().get("Token"), "验证Token信息");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_WeiBoUserReg_002() {
		Logger.start(true,"输入已关联过的用户(sWeiBoUserId已存在) ，则用户直接登陆成功");
		try {
			regBean.setSWeiboUserId("BindWeibo_5728053");
			regBean.setSName("atuoTest");
			regBean.setSMail(TestDataManager.getEmail());
			regBean.setSMobile(TestDataManager.getPhoneNum());
			regBean.setClientIp("120.1.1.1");
			regBean.setDAddTime(YMTDateUtil.getBeforeOrNextDay(-1));
			regBean.setTerminalSource("ios");
			regBean.setRegSource(1);
			regCall.setData(regBean);
			regCall.callService();		
			
			regCall.setData(regBean);
			regCall.callService();			
			Logger.verifyEquals("登录成功", regCall.getString("SuccessMessage"), "验证返回的Message信息");
			Logger.verifyEquals(ECode.SUCCESS, regCall.getString("ResponseCode"), "验证code");
			Logger.verifyEquals("true", regCall.getString("Success"), "验证是否成功");
			Logger.verifyEquals(14d, regCall.getResult().get("VerifyCode"), "验证凭据(14表示操作成功)");
			Logger.verifyIsNull(regCall.getResult().get("Remark"), "验证remark信息");
			
			Map userToken = userTokenWapper.getUserTokenByUid(((Double)regCall.getResult().get("UserId")).intValue());
			Logger.verifyEquals(userToken.get("sId"), regCall.getResult().get("Token"), "验证Token信息");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_WeiBoUserReg_003() {
		Logger.start(false,"不输入微博用户Id，用户创建失败");
		try {
			regBean.setSName("atuoTest");
			regBean.setSMail(TestDataManager.getEmail());
			regBean.setSMobile(TestDataManager.getPhoneNum());
			regBean.setClientIp("120.1.1.1");
			regBean.setDAddTime(YMTDateUtil.getBeforeOrNextDay(-1));
			regBean.setTerminalSource("ios");
			regBean.setRegSource(1);
			regCall.setData(regBean);
			regCall.callService();		
			
			regCall.setData(regBean);
			regCall.callService();			
			Logger.verifyEquals("false", regCall.getString("Success"), "验证是否成功");
			Logger.verifyEquals("dto验证失败", regCall.getString("LastErrorMessage"), "验证返回的Message信息");
			Logger.verifyEquals(ECode.ERROR400, regCall.getString("ResponseCode"), "验证code");
			Logger.verifyEquals(-1d, regCall.getResult().get("UserId"), "验证用户ID");
			Logger.verifyIsNull(regCall.getResult().get("Token"), "验证Token信息");
			Logger.verifyEquals(30d, regCall.getResult().get("VerifyCode"), "验证凭据(30表示新浪微博Id为空)");
			Logger.verifyIsNull(regCall.getResult().get("Remark"), "验证remark信息");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_WeiBoUserReg_004(){
		Logger.start(true,"不输入邮箱和手机号，注册成功");
		try {
			regBean.setSWeiboUserId("BindWeibo_"+TestDataManager.getRandomNum(10));
			regBean.setSName("atuoTest");
			regBean.setClientIp("120.1.1.1");
			regBean.setDAddTime(YMTDateUtil.getBeforeOrNextDay(-1));
			regBean.setTerminalSource("ios");
			regBean.setRegSource(1);
			regCall.setData(regBean);
			regCall.callService();		
			
			regCall.setData(regBean);
			regCall.callService();			
			Logger.verifyEquals("登录成功", regCall.getString("SuccessMessage"), "验证返回的Message信息");
			Logger.verifyEquals(ECode.SUCCESS, regCall.getString("ResponseCode"), "验证code");
			Logger.verifyEquals("true", regCall.getString("Success"), "验证是否成功");
			Logger.verifyEquals(14d, regCall.getResult().get("VerifyCode"), "验证凭据(14表示操作成功)");
			Logger.verifyIsNull(regCall.getResult().get("Remark"), "验证remark信息");
			
			Map userToken = userTokenWapper.getUserTokenByUid(((Double)regCall.getResult().get("UserId")).intValue());
			Logger.verifyEquals(userToken.get("sId"), regCall.getResult().get("Token"), "验证Token信息");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}
